package com.sogoodsoft.web.webservice.handler;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;

import org.apache.axis.AxisFault;
import org.apache.axis.MessageContext;
import org.apache.axis.handlers.BasicHandler;
import org.apache.axis.transport.http.HTTPConstants;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class RequestLogHandler extends BasicHandler {
    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    protected Logger logger = LoggerFactory.getLogger(getClass());

    @Override
    public void invoke(MessageContext msg) throws AxisFault {
        // TODO Auto-generated method stub
        if (logger.isWarnEnabled()) {
            Date date = new Date();
            String service = msg.getTargetService();
            String method = msg.getOperation().getName();
            String requestStr = msg.getRequestMessage().getSOAPPartAsString();
            MessageContext mc = MessageContext.getCurrentContext();
            HttpServletRequest request = (HttpServletRequest) mc.getProperty(HTTPConstants.MC_HTTP_SERVLETREQUEST);
            StringBuilder strbuilder = new StringBuilder("\n at ");
            strbuilder.append(date);
            strbuilder.append(":");
            strbuilder.append("IP ");
            strbuilder.append(request.getRemoteAddr());
            strbuilder.append(" call service ");
            strbuilder.append(service);
            strbuilder.append(".");
            strbuilder.append(method);
            strbuilder.append("\n Request SOAP Message :\n");
            strbuilder.append("**************************************************************************\n");
            strbuilder.append(requestStr);
            strbuilder.append("\n**************************************************************************\n");
            logger.warn(strbuilder.toString());
        }
    }

}
